(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.mgl.logController", logController);
	logController.$inject = [ "$scope","$filter", "$q", "modalService", "gridService", "layoutConfig", "validatorConfig", "authService", "mglService", 'ajaxServiceFactory' ];
	function logController($scope,$filter, $q, modalService, gridService, layoutConfig, validatorConfig, authService, mglService, ajaxServiceFactory) {
		var ctrl = this, ajaxService = ajaxServiceFactory.buildAjaxService({
			getTxnTypeOptions : {
				url : 'do/cls/dataList/txnType',
				method : 'post'
			}
		});
		ctrl.validatorConfig = validatorConfig;
		ctrl.mgl = {};
		ctrl.mgl.mglentrydetails = [];
		ctrl.effectiveDateReadonly = false;

		ctrl.init = function() {
			ajaxService.getTxnTypeOptions().then(function(response) {
				if(response) ctrl.txnTypeOptions = response.data;
				if( ctrl.txnTypeOptions &&  ctrl.txnTypeOptions.length > 0) {
					for (var i = 0; i < ctrl.txnTypeOptions.length; i++){
						if (ctrl.txnTypeOptions[i].value == 'REPAY'
								|| ctrl.txnTypeOptions[i].value == 'AMD'
								|| ctrl.txnTypeOptions[i].value == 'MGL'
								|| ctrl.txnTypeOptions[i].value == 'R') {
							ctrl.txnTypeOptions.splice(i, 1);
							i--;
						}
					}
				}
			});
			if (ctrl.validatorConfig)
				layoutConfig.formConfigs.mglDetail.tableConfigs.table.validatorConfig = ctrl.validatorConfig.mglentrydetails;
			ctrl.mglentrydetailsGridOptions = gridService.createInnerGridOptions($scope, layoutConfig.formConfigs.mglDetail.tableConfigs.table, null);
			ctrl.mglentrydetailsGridOptions.data = "vm.mgl.mglentrydetails";

			mglService.initDataOptions().then(function(optionsData) {
				ctrl.glCcyOptipns = optionsData.currency;
				ctrl.txnPartyTypeOptions = optionsData.allocCode;
				authService.logInit().then(function(response) {
					ctrl.mgl = response.data || {};
					ctrl.layoutConfig = layoutConfig;
					if (ctrl.mgl.txnType) {
						ctrl.txnType = ctrl.mgl.txnType;
					}
					if (!ctrl.mgl.mglentrydetails) {
						ctrl.mgl.mglentrydetails = [];
					}
				});

			});
		};
		ctrl.txnTypeChanged = function (mgl, form, isInit) {
			if(isInit){
				return;
			}
			var txnIdConfig = ctrl.layoutConfig.formConfigs.txnInfo.gridConfigs.txnId;
			if (mgl.txnType == 'ISSUE') {
				txnIdConfig.options.id = "cls.lookupTxnRefList";
				txnIdConfig.options.params = {
					"lockFlag" : "N",
					"loanStatusList" : [ "N", "P", "Q" ]
				};
			} else if (mgl.txnType == 'REPAY') {
				txnIdConfig.options.id = "cls.lookupRepaymentList";
				txnIdConfig.options.params = {
					"repayStatus" :  "R"
				};
			} else if (mgl.txnType == 'CUSTPAY') {
				txnIdConfig.options.id = "cls.lookupCustPayList";
				txnIdConfig.options.params = {
					"custpayStatus" : "R" 
				};
			} else if (mgl.txnType == 'DCHG') {
				txnIdConfig.options.id = "cls.lookupDChargeList";
				txnIdConfig.options.params = {
					"dchargeStatus" : "R"
				};
			} else if (mgl.txnType == 'AMD') {
				txnIdConfig.options.id = "cls.lookupLoanAmdList";
				txnIdConfig.options.params = {
					"amdStatus" :  "R" 
				};
			} else {
				/*modalService.openErrorDialogs({
					"messages" : "not supported"
				});
				mgl.txnType = 'ISSUE';*/
			}
			// clean data
			ctrl.mgl.txnId = "";
			ctrl.mgl.custCode = "";
			ctrl.mgl.custName = "";
			ctrl.mgl.txnPartyType = "";
			ctrl.mgl.prodCode = "";
			mglService.resetMglentrydetailsCustCode(ctrl.mgl);
		}
		
		ctrl.txnIdLookUp = {
			"id" : "cls.lookupTxnRefList",
			"params" : {
				"lockFlag" : "N",
				"loanStatusList" : [ "N", "P", "Q" ]
			},
			"size" : [ 1144 ],
			"callback" : {
				"ok" : selectLoanId
			},
			"manualInput" : false,
			"okEvent" : true,
			"cancelEvent" : false
		};
		
		ctrl.addRow = function(row) {
			return mglService.addRow(row, ctrl.mgl);
		};
		
		function selectLoanId(param) {
			if (ctrl.mgl.txnType == 'REPAY') {
				ctrl.mgl.txnId = param[0].repayId;
				mglService.getLoanInfoByLoanId($filter("encode")(param[0].loanId)).then(function(loanInfo){
					ctrl.customer = loanInfo.custCode;
					ctrl.custName = loanInfo.custName;
					ctrl.obligor = loanInfo.obligorCode;
					ctrl.obligorName = loanInfo.obligorName;
					ctrl.product = loanInfo.prodCode;
				});
			} else if(ctrl.mgl.txnType == 'ISSUE') {
				ctrl.mgl.txnId = param[0].loanId;
				ctrl.customer = param[0].custCode;
				ctrl.custName = param[0].custName;
				ctrl.obligor = param[0].obligorCode;
				ctrl.obligorName = param[0].obligorName;
				ctrl.product = param[0].prodCode;
			} else if(ctrl.mgl.txnType == 'CUSTPAY') {
				ctrl.mgl.txnId = param[0].custpayId;
				ctrl.customer = param[0].payeeCode;
				ctrl.custName = param[0].payeeName;
				ctrl.obligor = param[0].payerCode;
				ctrl.obligorName = param[0].payerName;
				ctrl.product = param[0].prodType;
			} else if(ctrl.mgl.txnType == 'DCHG') {
				ctrl.mgl.txnId = param[0].dchargeId;
				ctrl.customer = param[0].custCode;
				ctrl.custName = param[0].custName;
				ctrl.obligor = param[0].obligorCode;
				ctrl.obligorName = param[0].obligorName;
				ctrl.product = param[0].prodType;
			} else if(ctrl.mgl.txnType == 'AMD') {
				ctrl.mgl.txnId = param[0].amdId;
				mglService.getLoanInfoByLoanId($filter("encode")(param[0].loanId)).then(function(loanInfo){
					ctrl.customer = loanInfo.custCode;
					ctrl.custName = loanInfo.custName;
					ctrl.obligor = loanInfo.obligorCode;
					ctrl.obligorName = loanInfo.obligorName;
					ctrl.product = loanInfo.prodCode;
				});
			}
			ctrl.mgl.txnPartyType = "";
			ctrl.mgl.prodCode = "";
			if (ctrl.mgl.txnPartyType) {
				ctrl.txnPartyTypeChanged(ctrl);
			}
		};

		ctrl.txnPartyTypeChanged = function(param, form, flag) {
			if (flag) {
				return;
			}
			if (param.txnPartyType == "CUSTOMER") {
				ctrl.mgl.custCode = ctrl.customer;
				ctrl.mgl.custName = ctrl.custName;
			} else if (param.txnPartyType == "OBLIGOR") {
				ctrl.mgl.custCode = ctrl.obligor;
				ctrl.mgl.custName = ctrl.obligorName;
			}else if (!param.txnPartyType){
				ctrl.mgl.custCode = "";
				ctrl.mgl.custName = "";
			}
			ctrl.mgl.prodCode = ctrl.product;
			mglService.resetMglentrydetailsCustCode(ctrl.mgl);
		};

		ctrl.save = function() {
			mglService.resetMglentrydetails(ctrl.mgl.mglentrydetails);
			return authService.save(ctrl.mgl).then(submitSuccessful, submitFailed);
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				"messages" : [ "title.dialog.successful.submit" ],
				"btnCallback" : {
					"close" : function() {
						$scope.go("cls.workflow.wfTxn.list");
					}
				},
				"redirections" : [ {
					"name" : "title.infobar.workflow.list",
					"state" : "cls.workflow.wfTxn.list",
					"params" : {}
				} ]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				"messages" : response.error
			});
		}

		ctrl.glAmtChanged = function(row, form, flag) {
			mglService.glAmtChanged(row, flag, ctrl.mgl);
		};

		ctrl.glAmtBChanged = function(row, form, flag) {
			mglService.glAmtBChanged(row, flag, ctrl.mgl);
		};

		ctrl.fxRateChanged = function(row, form, flag) {
			mglService.fxRateChanged(row, flag, ctrl.mgl);
		};

		ctrl.acctCodechanged = function(row, form, firstLoad) {
			mglService.acctCodechanged(row, firstLoad, ctrl.mgl);
		};

		ctrl.glCcyChanged = function(row, form, flag) {
			mglService.glCcyChanged(row, flag, ctrl.mgl);
		};

	}
})(angular);
